package com.jz.baiduHotSearch.service;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jz.baiduHotSearch.mapper.HotSearchInfoMapper;
import com.jz.baiduHotSearch.pojo.HotBranch;
import com.jz.baiduHotSearch.pojo.HotInfo;
import com.jz.baiduHotSearch.pojo.HotSearchDO;
import com.jz.baiduHotSearch.pojo.HotSearchInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Host;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 热搜service
 */
@Slf4j
@Service
public class HotSearchInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(HotSearchInfoService.class);

    @Autowired
    private HotSearchInfoMapper hotSearchInfoMapper;

    /**
     * 记录热搜信息
     */
    public void recordHotSearchInfo() throws IOException {
        LOGGER.info("记录热搜信息-----------------");
        List<HotSearchInfo> hotSearchInfoList = this.getHotSearchInfo();
        //更新热点信息
        List<HotInfo> hotInfoList = hotSearchInfoList.stream().map(item -> {
            HotInfo hotInfoSearch = hotSearchInfoMapper.findHotInfo(item.getQuery());
            //1.若不存在，添加
            if(hotInfoSearch == null){
                HotInfo hotInfo = new HotInfo();
                hotInfo.setQuery(item.getQuery());
                hotInfo.setImg(item.getImg());
                hotInfo.setWord(item.getWord());
                hotInfo.setDesc(item.getDesc());
                hotInfo.setUrl(item.getUrl());
                return hotInfo;
            }else{
                //2.若存在，描述不存在，更新描述
                if(!item.getDesc().equals("")&&(hotInfoSearch.getDesc()==null||hotInfoSearch.getDesc().equals(""))){
                    int count = hotSearchInfoMapper.upateHotInfo(item.getQuery(), item.getDesc());
                    LOGGER.info("更新热点信息详情" + count);
                }
            }
            return null;
        }).collect(Collectors.toList());
        //过滤掉空值
        List<HotInfo> hotInfoListFilter = hotInfoList.stream().filter(item -> {
            if (item == null) {
                return false;
            } else {
                return true;
            }
        }).collect(Collectors.toList());
        //入库
        if(hotInfoListFilter.size()>0){
            int hotInfoCount = hotSearchInfoMapper.insertHotInfo(hotInfoListFilter, new Date());
            LOGGER.info("热搜信息更新:" + hotInfoCount);
        }
        //保存批次信息
        List<HotBranch> hotBranchList = hotSearchInfoList.stream().map(item -> {
            HotBranch hotBranch = new HotBranch();
            hotBranch.setHotInfoId(hotSearchInfoMapper.findHotInfo(item.getQuery()).getId());
            hotBranch.setHotScore(item.getHotScore());
            hotBranch.setIndex(item.getIndex());
            return hotBranch;
        }).collect(Collectors.toList());
        if(hotBranchList.size()>0){
            int branchCount = hotSearchInfoMapper.insertHotBranch(hotBranchList,System.currentTimeMillis(),new Date());
            LOGGER.info("热搜批次更新:" + branchCount);
        }
    }

    /***
     * 查询热搜消息
     *
     * @param query
     * @return
     */
    public List<HotSearchDO> findHotInfoList(String query){
        LOGGER.info("查询热搜信息-----------------");
        query = query.trim();
        List<HotSearchDO> hotInfoList = hotSearchInfoMapper.hotSearchListQuery(query);
        return hotInfoList;
    }


    public List<HotSearchDO> findHotInfoSortList(String keyWord,String startDate,String endDate){
        List<HotSearchDO> hotInfoList = hotSearchInfoMapper.findHotInfoSortList(keyWord,startDate,endDate);
        return hotInfoList;
    }

    /***
     * 查询信息历史
     *
     * @param query
     * @param id
     * @return
     */
    public Map<String, Object> findHotInfoHistoryList(String query,String id){
        LOGGER.info("查询热搜信息历史-----------------");
        query = query.trim();
        if (StringUtils.isEmpty(query) && StringUtils.isEmpty(id)) {
            return null;
        }
        List<Map<String, Object>> hotInfoList = hotSearchInfoMapper.findHotInfoHistoryList(query,id);
        //X时间轴的数据
        List<Object> createDateList = hotInfoList.stream().map(item -> item.get("createDate")).collect(Collectors.toList());
        //Y热搜指数的数据
        List<Object> hotScoreList = hotInfoList.stream().map(item -> item.get("hotScore")).collect(Collectors.toList());
        List<Object> indexList = hotInfoList.stream().map(item -> item.get("index")).collect(Collectors.toList());
        Map resMap = new HashMap();
        resMap.put("data",hotInfoList);
        resMap.put("xData",createDateList);
        resMap.put("yData",hotScoreList);
        resMap.put("yIndexData",indexList);
        return resMap;
    }

    /**
     * 获取百度热点信息
     * @return
     * @throws IOException
     */
    private List<HotSearchInfo> getHotSearchInfo() throws IOException {
        //1.获取热搜页面
        String url = "https://top.baidu.com/board?tab=realtime";
        Document document = Jsoup.connect(url).get();
        //2.获取返回结果信息
        Elements select = document.select("#sanRoot");
        String comment = select.comments().get(0).toString();
        //3.结果信息转换为复合json的格式
        String res = comment.replaceAll("<!--", "")
                .replaceAll("-->", "")
                .replaceAll("s-data:","");
        //4.解析json字符串
        JSONObject jsonObject = JSON.parseObject(res);
        JSONArray cards = jsonObject.getJSONObject("data").getJSONArray("cards");
        JSONObject cards01 = cards.getJSONObject(0);
        JSONArray content = cards01.getJSONArray("content");
        //5.将json对象转换为相应的对象
        List<HotSearchInfo> hotSearchInfoList = JSONObject.parseArray(content.toJSONString(), HotSearchInfo.class);
        LOGGER.info("当前热搜有"+hotSearchInfoList.size()+"条！");
        return hotSearchInfoList;
    }

    @Transactional
    public void xlwbHotSearchInsert(){
        List<HotSearchDO> hotSearchDOS = grabXlwbHotSearchData();
        log.info("数据抓取完成");
        for (HotSearchDO item : hotSearchDOS) {
            hotSearchInfoMapper.hotSearchInsert(item);
        }
    }

    private List<HotSearchDO> grabXlwbHotSearchData() {
        List<HotSearchDO> resList = new ArrayList<>();
        String url = "https://s.weibo.com/top/summary?cate=realtimehot";
        String cookie = "SUB="+"_2AkMS31Hef8NxqwFRmfoWyGLjaYl0yAnEieKkg6AFJRMxHRl-yT9kqmoetRB6OV9_MU4pb1XZaTreoICatRorx2k6O6DF"+"; SUBP="+"0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFG.5MMI2zWR6IO9iQdcHoI"+";";
        String responseHtml = HttpRequest.get(url).header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
                .header("Cookie", cookie).timeout(3000).execute().body();
        Document document = Jsoup.parse(responseHtml);
        Element tbody = document.getElementsByTag("tbody").get(0);
        Elements tr = tbody.getElementsByTag("tr");
        for (Element item : tr) {
            HotSearchDO hotItem = new HotSearchDO();
            Element indexElement = item.select(".td-01").get(0);
            //排序
            String index = indexElement.text();
            if(!index.matches("\\d+")){
                continue;
            }
            //热搜关键词
            Element contentElement = item.select(".td-02").select("a").get(0);
            String text = contentElement.text();
            //热度指数
            Elements hotElements = item.select(".td-02").select("span");
            String hot = "";
            String keyword = "";
            if(!CollectionUtils.isEmpty(hotElements)){
                String hotRes = hotElements.get(0).text();
                hot = ReUtil.get("\\d+",hotRes,0);
                keyword = ReUtil.get("[^\\x00-\\xff]+",hotRes,0);
            }
//            String res = index+"\t"+hot+"\t"+text;
            if(StrUtil.isNotEmpty(keyword)){
                //添加关键词，如：【综艺】【音乐】等
                text = "【"+keyword+"】"+text;
            }
            if(StrUtil.isEmpty(text)){
                continue;
            }
            hotItem.setNmSort(Integer.valueOf(index));
            hotItem.setNmIndex(Long.valueOf(hot));
            hotItem.setStContent(text);
            hotItem.setStPlatform("新浪微博");
            hotItem.setDtCreateDate(new Date());
            resList.add(hotItem);
        }
//        log.info(resList.toString());
        return resList;
    }
}
